package com.linkzd.iot.service.Impl;

import java.util.ArrayList;
import java.util.List;

import com.linkzd.common.core.domain.AjaxResult;
import com.linkzd.common.core.domain.entity.SysDictData;
import com.linkzd.common.utils.DateUtils;
import com.linkzd.iot.model.DeviceTypeModel;
import com.linkzd.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.linkzd.iot.mapper.DeviceTypeMapper;
import com.linkzd.iot.domain.DeviceType;
import com.linkzd.iot.service.IDeviceTypeService;

/**
 * 设备类型Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-18
 */
@Service
public class DeviceTypeServiceImpl implements IDeviceTypeService 
{
    @Autowired
    private DeviceTypeMapper deviceTypeMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 查询设备类型
     * 
     * @param deviceTypeId 设备类型主键
     * @return 设备类型
     */
    @Override
    public DeviceType selectDeviceTypeByDeviceTypeId(Long deviceTypeId)
    {
        return deviceTypeMapper.selectDeviceTypeByDeviceTypeId(deviceTypeId);
    }

    /**
     * 查询设备类型列表
     * 
     * @param deviceType 设备类型
     * @return 设备类型
     */
    @Override
    public List<DeviceType> selectDeviceTypeList(DeviceType deviceType)
    {
        return deviceTypeMapper.selectDeviceTypeList(deviceType);
    }

    /**
     * 新增设备类型
     * 
     * @param deviceType 设备类型
     * @return 结果
     */
    @Override
    public int insertDeviceType(DeviceType deviceType)
    {
//        DeviceType newType=new DeviceType();
//        newType.setDeviceType(deviceType.getDeviceType());
//        List<DeviceType> list=deviceTypeMapper.selectDeviceTypeList(newType);
//        if(list != null || list.size() != 0)
//            return AjaxResult.warn("添加设备类型已存在，请重新添加");
//        deviceType.setCreateTime(DateUtils.getNowDate());
//        int ret =deviceTypeMapper.insertDeviceType(deviceType);
//        return ret>0?AjaxResult.success():AjaxResult.error();
        return deviceTypeMapper.insertDeviceType(deviceType);
    }

    /**
     * 修改设备类型
     * 
     * @param deviceType 设备类型
     * @return 结果
     */
    @Override
    public int updateDeviceType(DeviceType deviceType)
    {
        deviceType.setUpdateTime(DateUtils.getNowDate());
        return deviceTypeMapper.updateDeviceType(deviceType);
    }

    /**
     * 批量删除设备类型
     * 
     * @param deviceTypeIds 需要删除的设备类型主键
     * @return 结果
     */
    @Override
    public int deleteDeviceTypeByDeviceTypeIds(Long[] deviceTypeIds)
    {
        return deviceTypeMapper.deleteDeviceTypeByDeviceTypeIds(deviceTypeIds);
    }

    /**
     * 删除设备类型信息
     * 
     * @param deviceTypeId 设备类型主键
     * @return 结果
     */
    @Override
    public int deleteDeviceTypeByDeviceTypeId(Long deviceTypeId)
    {
        return deviceTypeMapper.deleteDeviceTypeByDeviceTypeId(deviceTypeId);
    }

    @Override
    public List<DeviceTypeModel> getDeviceTypesByVariety(String type) {
        Long[] types =  deviceTypeMapper.getDeviceTypesByVariety(type);
        List<SysDictData> sysDictDataList = sysDictDataMapper.selectDictDataByTypeVariety(types);
        List<DeviceTypeModel> deviceTypeModelList = new ArrayList<>();
        for (SysDictData sysDictData:sysDictDataList){
            deviceTypeModelList.add(new DeviceTypeModel(Integer.parseInt(sysDictData.getDictValue()),sysDictData.getDictLabel()));
        }
        return deviceTypeModelList;
    }
}
